package aar.test.tessocr.fragment;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import aar.test.tessocr.EventMessage.EventMessageBase;
import butterknife.ButterKnife;
import butterknife.Unbinder;

/**
 * Created by sunshine on 2018/6/4.
 */

public abstract class BaseFragment extends Fragment {
    private int layoutRes;
    private Unbinder unbinder;

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        setUpContentView();
        View view = setContentView(inflater, container, savedInstanceState);
        unbinder = ButterKnife.bind(this, view);
        if (!EventBus.getDefault().isRegistered(this)) {
            EventBus.getDefault().register(this);
        }
        setUpView(view);
        setUpComponent();
        setUpData();
        return view;
    }

    protected View setContentView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return inflater.inflate(layoutRes, container, false);
    }

    protected abstract void setUpContentView();

    protected void setContentView(int layoutRes) {
        this.layoutRes = layoutRes;
    }

    protected abstract void setUpView(View view);

    protected abstract void setUpComponent();

    protected abstract void setUpData();

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
        EventBus.getDefault().unregister(this);
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onGetEventBusMessageOne(EventMessageBase eventMsg) {
        //空实现
    }
}
